package com.software.adapterview.wechat;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.software.adapterview.R;
import com.software.adapterview.adapters.WechatFragmentAdapter;
import com.software.adapterview.fragments.ContractFragment;
import com.software.adapterview.fragments.FindFragment;
import com.software.adapterview.fragments.MineFragment;
import com.software.adapterview.fragments.WechatFragment;

import java.util.ArrayList;
import java.util.List;

public class WechatActivity extends AppCompatActivity {

    private TabLayout tb_nav;
    private ViewPager2 vp_context;
    private List<Fragment> fragmentList;
    private List<String> tabNameList;
    private List<Integer> imageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wechat);
        //获取activity中的控件
        initViews();

        initFragments();

        //创建适配器
        WechatFragmentAdapter adapter = new WechatFragmentAdapter(
                fragmentList, this
        );
        //绑定ViewPager2和适配器
        vp_context.setAdapter(adapter);
        //tabLayout与ViewPager2关联
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
                tb_nav,
                vp_context,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                       tab.setText(tabNameList.get(position));
                       tab.setIcon(imageList.get(position));
                    }
        }
        );
        //使效果生效
        tabLayoutMediator.attach();
    }

    private void initFragments() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new WechatFragment());
        fragmentList.add(new ContractFragment());
        fragmentList.add(new FindFragment());
        fragmentList.add(new MineFragment());

        tabNameList = new ArrayList<>();
        tabNameList.add("微信");
        tabNameList.add("通讯录");
        tabNameList.add("发现");
        tabNameList.add("我");

        imageList = new ArrayList<>();
        imageList.add(R.mipmap.message);
        imageList.add(R.mipmap.contract);
        imageList.add(R.mipmap.find);
        imageList.add(R.mipmap.mine);

    }

    private void initViews() {
        tb_nav = findViewById(R.id.tb_nav);
        vp_context = findViewById(R.id.vp_context);
    }
}